<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;

class Index extends Controller
{

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $header = '上海宏璞网络科技';
        $this->assign('header',$header);

        $action = $this->request->action();
        $menu = [
            'index' => 1,
            'about' => 2,
            'product' => 3,
            'news' => 4,
            'news_info' => 4,
            'connect' => 5,
        ];
        if(!empty($menu[$action])){
            $this->assign('menu_id',$menu[$action]);
        }else{
            $this->assign('menu_id',1);
        }
    }


    public function index()
    {
        $data = Db::table('news')->where('status',1)->order('created_at desc')->limit(4)->select();
        $this->assign('news',$data);
       return $this->fetch('index/index');
    }

    public function about()
    {
       return $this->fetch('index/about');
    }

    public function product()
    {
        $this->assign('data',[1,2,3,4,5,6,7]);
       return $this->fetch('index/product');
    }

    public function news()
    {
        $data = Db::table('news')->where('status',1)->order('created_at desc')->select();
        $this->assign('data',$data);
       return $this->fetch('index/news');
    }

    public function news_info()
    {
       $param = $this->request->param();
       $data = Db::table('news')->where('id',$param['id'])->find();
       $this->assign('data',$data);
       return $this->fetch('index/news_info');
    }

    public function connect()
    {
        $data = Db::table('config')->where('id',1)->find();
        $this->assign('data',$data);
       return $this->fetch('index/connect');
    }

    public function inputConnect()
    {
        $param = $this->request->param();
        $data = [
            'name' => htmlspecialchars($param['name']),
            'phone' => htmlspecialchars($param['phone']),
            'content' => htmlspecialchars($param['content']),
            'created_at' => time()
        ];
        $res = Db::table('connect')->insert($data);
        if ($res) {
            return $this->success([],'提交成功,我们将尽快与您联系');
        }else{
            return $this->error([],'提交失败');
        }
    }
}
